<?php

namespace App\Store\Models\Goods;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsBrand;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Basic\GoodsCraft;
use App\Store\Models\Basic\GoodsPricetype;
use App\Store\Models\Basic\GoodsStone;
use App\Store\Models\Basic\GoodsStyle;
use App\Store\Models\Basic\PaytypeList;
use App\Store\Models\Basic\Supply;
use Illuminate\Database\Eloquent\Model;

class GoodsReplenishInfolist extends Model
{
    protected $table = 'site_goods_replenish_infolist';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 获取列表
     * type 1补货汇总 2款式汇总 3补货明细
     */
    public static function getList($request)
    {
        $query = self::where('p_id', $request['id']);

        // 筛选
        $goods_category_id = $request['goods_category_id'] ?? 0;
        if ($goods_category_id != 0) {
            $query->where('goods_category_id', $goods_category_id);
        }
        $goods_style_id = $request['goods_style_id'] ?? 0;
        if ($goods_style_id != 0) {
            $query->where('goods_style_id', $goods_style_id);
        }
        $audit_status = $request['audit_status'] ?? 0;
        if ($audit_status != 0) {
            $query->where('audit_status', $audit_status);
        }
        $goods_sn_code = $request['goods_sn_code'] ?? '';
        if ($goods_sn_code != '') {
            $query->where('goods_sn_code', $goods_sn_code);
        }
        $query->orderBy('create_at', 'desc');

        $list = $query->get()->toArray();
        return ['list' => $list];
    }

    /**
     * 导出
     */
    public static function getExportList($request)
    {
        $id = $request['id'];
        $id = is_array($id) ? $id : (is_array(json_decode($id, true)) ? json_decode($id, true) : []);

        $ids = $request['ids'];
        $ids = is_array($ids) ? $ids : (is_array(json_decode($ids, true)) ? json_decode($ids, true) : []);

        $cp_id= $request['cp_id'] ?? 0;
        $query = self::with(['storeinfo', 'goods_category', 'supply', 'goods_style', 'goods_craft', 'goods_pricetype', 'goods_stone', 'paytype_info'])->where(['is_del'=>1,'cp_id'=>$cp_id]);
        $store_id = $request['store_id']??0;
        if ($store_id>0) {
            $query->where('store_id',$store_id);
        }else{
            $store_ids = StoreCommon::getCurrentAccountStoreId($request);
            if($store_ids){//权限控制
                if(is_array($store_ids)){//获取当前权限下的门店
                    $query->whereIn('store_id',$store_ids);
                }
                if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                    $query->where('store_id', intval($store_ids));
                }
            }
        }

        if (count($id) > 0) {
            $query = $query->whereIn('p_id', $id);
        }

        if (count($ids) > 0) {
            $query = $query->whereIn('id', $ids);
        }
        $list = $query->get()->toArray();

        return ['list' => $list];
    }

    /**
     * 类别
     */
    public function goods_category()
    {
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id', 'category_name','is_gram');
    }

    /**
     * 款式
     */
    public function goods_style()
    {
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id')->select('id', 'style_name');
    }

    /**
     * 店铺
     */
    public function storeinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id', 'store_name','store_file_path','tracing_url','qrcode_url','brand_url');
    }

    /**
     * 供应商
     */
    public function supply()
    {
        return $this->hasOne(Supply::class, 'id', 'supply_id')->select('id', 'supply_name');
    }

    public function goods_craft()
    {
        return $this->hasOne(GoodsCraft::class, 'id', 'goods_craft_id')->select('id', 'craft_name');
    }
    public function goods_pricetype()
    {
        return $this->hasOne(GoodsPricetype::class, 'id', 'goods_pricetype_id')->select('id', 'pricetype_name');
    }
    public function goods_stone()
    {
        return $this->hasOne(GoodsStone::class, 'id', 'goods_stone_id')->select('id', 'stone_name');
    }
    public function paytype_info()
    {
        return $this->hasOne(PaytypeList::class, 'id', 'goods_stone_id')->select('id', 'paytype_name');
    }

    /**
     * 品牌
     */
    public function goods_brand()
    {
        return $this->hasOne(GoodsBrand::class, 'id', 'goods_brand_id')->select('id', 'brand_name');
    }

    /**
     * 品牌
     */
    public function brand()
    {
        return $this->hasOne(GoodsBrand::class, 'id', 'goods_brand_id')->select('id', 'brand_name');
    }
}
